using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Pampa.Common.Pages;

namespace Pampa.WebControls
{
    public abstract class BaseControl : WebControl
    {
        protected string clientClass = "undefined";

        #region Render

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute("id", this.ID);
            writer.AddAttribute("class", this.CssClass);

            if (!string.IsNullOrEmpty(this.Height.ToString()))
                writer.AddStyleAttribute("height", this.Height.ToString());
            if (!string.IsNullOrEmpty(this.Width.ToString()))
                writer.AddStyleAttribute("width", this.Width.ToString());

            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            RenderContents(writer);

            writer.RenderEndTag();
        }

        #endregion

        #region Register

        protected override void OnLoad(EventArgs e)
        {
            RegisterLibrary("Pampa.Common/Common.js");

            base.OnLoad(e);
        }
        
        protected void RegisterStyleSheet(string url)
        {
            AbstractPage.RegisterStyleSheet(this.Page, url);
        }

        protected void RegisterLibrary(string url)
        {
            AbstractPage.RegisterLibrary(this.Page, url);
        }

        protected static string GetResourceUrl(string url)
        {
            return AbstractPage.GetResourceUrl(url);
        }

        protected static string GetImagesPath()
        {
            return AbstractPage.GetImagesPath();
        }

        #endregion 
        
        #region MVC Hook

        public virtual string Render()
        {
            var stringWriter = new StringWriter();
            var textWriter = new HtmlTextWriter(stringWriter);
            
            // control content writing in the response
            this.RenderControl(textWriter);
            
            return stringWriter.ToString();
        }

        protected static string IncludeStyle(string url)
        {
            return "<link href=\"" + GetResourceUrl(url) + "\" rel=\"stylesheet\" type=\"text/css\" />\n";
        }

        protected static string IncludeLibrary(string url)
        {
            return "<script src=\"" + GetResourceUrl(url) + "\" type=\"text/javascript\"></script>\n";
        }

        #endregion
    }
}
